﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper;
using System.Drawing;
using BaseHelper.Base;
using DeviceSDK.LED.ZhongHai.ZhongHai;


namespace DeviceSDK.LED.ZhongHai
{
    public class ZhongHai_V1
    {
 
        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";

                case 1:
                    return "操作失败！";

                case 2:
                    return "发送节目单失败！";
                    
                case -1:
                    return "操作失败！";

                default:
                    return "未知错误！";
            }
        }



        public static ZHONGHAI_SDK Connect(string IP, int Port)
        {

            ZHONGHAI_SDK zh = new ZHONGHAI_SDK();
            if (zh.Connect(IP, Port))
            {
                return zh;
            }
            return null;
        }



        public static int Get_PlaybillContent(string IP,int Port, ref string Content)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            Content = zh.GetPlaybillContent();

            zh.Close();

            return Content == null ? 1 : 0;
        }



        //获得-屏幕状态
        public static int Get_Screen_Status(string IP, int Port, ref int Value)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            Value = zh.GetScreenState();

            zh.Close();
            return 0;
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP,int Port)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            int ret = zh.SetScreenState(false) ? 0 : 1;

            zh.Close();
            return ret;

        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP, int Port)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            int ret = zh.SetScreenState(true) ? 0 : 1;

            zh.Close();
            return ret;
        }

        //获得-屏幕亮度
        public static int Get_Brightness(string IP,int Port, ref int Value)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            Value = zh.GetBrightness();

            zh.Close();
            int ret = Value == -1 ? 1 : 0;

            return ret;
        }

        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Port, int Value)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            if (zh == null)
                return 1;

            int ret = zh.SetBrightness(Value) ? 0 : 1;

            zh.Close();

            return ret;
        }


        //发送-节目
        public static LEDSendResult Send_Programs(string IP, int Port, int Width, int Height, PlaybillProgramInfo[] Programs, bool isCheckResult)
        {
            ZHONGHAI_SDK zh = Connect(IP, Port);
            LEDSendResult result = new LEDSendResult(isCheckResult);
            if (zh == null)
            {
                result.RetCode = 1;
                return result;
            }
            int ret = 1;
            
            PlayBill pb = new PlayBill();

            foreach (PlaybillProgramInfo pinfo in Programs)
            {

                switch (pinfo.Type)
                {
                    case "text":
                        pb.AddString(pinfo.Text, pinfo.PlayTime, pinfo.FontName, pinfo.FontSize, pinfo.FontColor, pinfo.Width, pinfo.Height);
                        break;
                }
            }

            if (zh.SendPlaybill(pb))
            {
                if (isCheckResult)
                {
                    PlaybillProgramInfo[] pbs = zh.DecodeContent(pb.Create());
                    if (pbs.Length == 0)
                        result.SendContent = "";
                    else
                    {
                        string content = "";
                        foreach (PlaybillProgramInfo p in pbs)
                        {
                            content += string.Format("{0},{1},{2},{3},{4},{5},{6}\r\n",
                                p.PlayTime, p.FontName, p.FontSize, p.FontColor, p.X, p.Y, p.Text
                                );
                        }
                        result.SendContent = content;
                    }

                    result.ReplayContent = zh.GetPlaybillContent();

                }
                ret = 0;
            }
            else
                ret = 2;

            result.RetCode = ret;
            zh.Close();
            return result;

        }
    }
}
